﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;

namespace SchemeManager
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window
    {
        private const string LOGIN_INFO = "输入用户名与密码登录到系统";
        private const string FIRST_LOGIN = "第一次登录，输入的用户名与密码将作为初始信息";

        User user = null;

        public Login()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Login_Loaded);
        }

        void Login_Loaded(object sender, RoutedEventArgs e)
        {
            //加载用户信息
            this.user = User.LoadUser();
            if (this.user == null)
            {
                MessageBox.Show("应用程序缺少必须组件，无法运行", "外部数据错误", MessageBoxButton.OK, MessageBoxImage.Error);
                Process.GetCurrentProcess().Kill();
            }

            if (this.user.IsEmpty())
            {
                labInfo.Text = FIRST_LOGIN;
                labInfo.Foreground = Brushes.Red;
            }
            else
            {
                labInfo.Text = LOGIN_INFO;
            }
            this.tbName.Focus();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (this.tbName.Text.Length < 1)
            {
                MessageBox.Show("请输入用户名");
                return;
            }

            if (this.tbPassword.Password.Length < 1)
            {
                MessageBox.Show("请输入密码");
                return;
            }

            //第一次，登录
            if (this.user.IsEmpty())
            {
                this.user.Name = this.tbName.Text.Trim();
                this.user.Password = this.tbPassword.Password.Trim();
                if (User.SaveUser(user) == false)
                {
                    MessageBox.Show("无法保存用户名与密码，请检查当前用户的权限");
                    return;
                }
                else
                {
                    this.DialogResult = true;
                }
            }
            else
            {
                if (this.user.Validate(this.tbName.Text.Trim(), this.tbPassword.Password.Trim()) == false)
                {
                    MessageBox.Show("用户名或者密码不正确");
                    return;
                }
                else
                {
                    this.DialogResult = true;
                }
            }

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void tbPassword_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                this.btnOk_Click(this.btnOk, new RoutedEventArgs());
            }
        }
    }
}
